package com.atguigu.tingshu.user.receiver;

import com.alibaba.fastjson.JSON;
import com.atguigu.tingshu.common.constant.KafkaConstant;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * @author atguigu-mqx
 * @ClassName UserReceiver
 * @description: TODO
 * @date 2024年06月21日
 * @version: 1.0
 */
@Component
public class UserReceiver {

    @Autowired
    private UserInfoService userInfoService;

    /**
     *  监听用户支付记录
     * @param record
     */
    @KafkaListener(topics = KafkaConstant.QUEUE_USER_PAY_RECORD)
    public void userPayRecord(ConsumerRecord<String,String> record){
        //  获取发送的数据.
        String strJson = record.value();
        //  判断
        if (!StringUtils.isEmpty(strJson)){
            //  数据类型专辑.
            UserPaidRecordVo userPaidRecordVo = JSON.parseObject(strJson, UserPaidRecordVo.class);
            //  调用服务层方法记录用户购买信息.
            userInfoService.userPayRecord(userPaidRecordVo);
        }
    }
}
